package com.bw.common.mvvm.viewmodel;

import android.os.Bundle;

import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.LogUtils;
import com.bw.common.bus.LiveDataBus;
import com.bw.common.entity.BaseEntity;
import com.bw.common.field.ActionEntity;
import com.bw.common.field.UIBusAction;
import com.bw.common.mvvm.model.BaseModel;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseViewModel<M extends BaseModel> extends ViewModel implements
        IViewModel, Observer<BaseEntity> {

    protected M m;
    protected CompositeDisposable disposable;//收集全部rxjava订阅，防止内存泄露
    private UILiveDataBus uiLiveDataBus = new UILiveDataBus();

    public UILiveDataBus getUiLiveDataBus() {
        return uiLiveDataBus;
    }

    @Override
    public void create() {
        disposable = new CompositeDisposable();
        createModel();
        initData();
    }

    //创建M对象
    private void createModel(){
        //反射获取泛型类型创建对象
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz = (Class) parameterizedType.getActualTypeArguments()[0];
        try {
            m = (M) clazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        disposable.dispose();
        disposable.clear();
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        disposable.add(d);
        showDialog();
    }

    protected abstract void initData();

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        if (baseEntity.status!=200&&baseEntity.status!=0){
            //showErro
            showErro(baseEntity.message);
            return;
        }
        result(baseEntity);
    }

    @Override
    public void onError(@NonNull Throwable e) {
        LogUtils.e("onError:"+e.getMessage());
        showErro("请求出错");
    }

    @Override
    public void onComplete() {
        disDialog();
    }

    protected abstract void result(BaseEntity entity);

    //启动界面->发送livedata事件
    protected void startPage(Class clazz){
        UIBusAction uiBusAction = new UIBusAction();
        uiBusAction.action = ActionEntity.STARTPAGE;
        Map<String,Object>map=new HashMap<>();
        map.put(ActionEntity.CLASSKEY,clazz);
        uiBusAction.parames = map;
        uiLiveDataBus.setDataBus(uiBusAction);
    }

    //启动界面->携参数
    protected void startPage(Class clazz, Bundle bundle){
        UIBusAction uiBusAction = new UIBusAction();
        uiBusAction.action = ActionEntity.STARTBUNDLEPAGE;
        Map<String,Object>map=new HashMap<>();
        map.put(ActionEntity.CLASSKEY,clazz);
        map.put(ActionEntity.BUNDLEKEY,bundle);
        uiBusAction.parames = map;
        uiLiveDataBus.setDataBus(uiBusAction);
    }

    //关闭界面
    protected void finishPage(){
        UIBusAction uiBusAction = new UIBusAction();
        uiBusAction.action = ActionEntity.FINISHPAGE;
        uiLiveDataBus.setDataBus(uiBusAction);
    }

    protected void showDialog(){
        UIBusAction uiBusAction = new UIBusAction();
        uiBusAction.action = ActionEntity.SHOWDIALOG;
        uiLiveDataBus.setDataBus(uiBusAction);
    }

    protected void disDialog(){
        UIBusAction uiBusAction = new UIBusAction();
        uiBusAction.action = ActionEntity.DISDIALOG;
        uiLiveDataBus.setDataBus(uiBusAction);
    }

    protected void showErro(String msg){
        UIBusAction uiBusAction = new UIBusAction();
        uiBusAction.action = ActionEntity.SHOWERRO;
        Map<String,String>map=new HashMap<>();
        map.put(ActionEntity.ERROKEY,msg);
        uiBusAction.parames = map;
        uiLiveDataBus.setDataBus(uiBusAction);
    }

    //livedata 事件驱动创建
    public static class UILiveDataBus{
        private LiveDataBus<UIBusAction>uiLiveDataBus=new LiveDataBus<>();
        public LiveDataBus<UIBusAction> getUiLiveDataBus() {
            return uiLiveDataBus;
        }
        public void setDataBus(UIBusAction action){
            uiLiveDataBus.setValue(action);
        }
    }

}
